<template>
	<view>
		<Navbar :showLeft="true" title="我的考试记录"></Navbar>
		<uni-section title="历史考试记录" type="line" v-if="status != 'loading' && !classes ">
			<uni-collapse accordion v-model="value" ref="collapse" @change="change">
				<uni-collapse-item v-for="(item, index) in historyExam" :title="item.time">
					<uni-list style="margin-left: 15px;">
						<uni-list-item v-for="(subItem, index) in item.children"
							:note="subItem.status == 1 ? '已完成' : '未完成'"
							:title="`${subItem.startTime}~${subItem.endTime}`">
							<template v-slot:footer>
								<button type="primary" size="mini" style="width: auto;height: 30px;"
									@click="showHistoryExam(subItem)">查看</button>
								<button type="warn" size="mini" style="width: auto;height: 30px;margin-left: 10px;"
									@click="delHistoryExam(subItem.examId)">删除</button>
							</template>
						</uni-list-item>
					</uni-list>
				</uni-collapse-item>
			</uni-collapse>
		</uni-section>
		<uni-load-more iconType="circle" :status="loadStatus" />
	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		getCurrentInstance,
		nextTick
	} from "vue";
	import {
		onLoad,
		onReachBottom
	} from "@dcloudio/uni-app";
	const {
		proxy
	} = getCurrentInstance();
	const loadStatus = ref("loading")
	const dataSource = ref({});
	const historyExam = ref({})
	const loadHistoryExam = async () => {
		let result = await proxy.Request({
			url: proxy.Api.loadHistoryExam
		});
		if (!result) return
		historyExam.value = result.data
		loadStatus.value = "noMore"
	}
	onLoad(() => {
		loadHistoryExam();
	});
	const showHistoryExam = async (item) => {
		console.log(item)
		let result = await proxy.Request({
			url: proxy.Api.loadHistoryExamById,
			params: {
				id: item.examId
			}
		});
		if (!result.data) {
			return;
		}
		result.data.examInfo.status = item.status
		result.data.examInfo.score = item.score
		uni.setStorageSync("examQuestionList", result.data.examQuestionList)
		uni.setStorageSync("examInfo", result.data.examInfo)
		loadStatus.value = "noMore"
		uni.navigateTo({
			url: "../exam/ExamQuestion?examId=" + result.data.examInfo.examId
		});
	}
	const delHistoryExam = async (id) => {
		uni.showModal({
			title: '提示',
			content: '你确定要删除吗',
			success: async function(res) {
				if (res.confirm) {
					let result = await proxy.Request({
						url: proxy.Api.delExam,
						params: {
							examId: id
						}
					});
					loadHistoryExam()
				} else if (res.cancel) {
					console.log('取消');
				}
			}
		})
	}
</script>

<style lang="scss" scoped>
</style>